<?php
	/**
	* Global controller
	*/
	class GlobalController extends Object
	{
		/**
		 * Model object for current controller
		 *
		 * @var Model
		 */
		public $controller_model = null;
		
		/**
		 * page title
		 *
		 * @var string
		 */
		public $page_title = null;
		
		/**
		 * Layout
		 *
		 * @var string
		 */
		public $layout = null;
		
		/**
		 * Helpers' array to be included
		 *
		 * @var array
		 */
		public $helpers = array();
		
		/**
		 * JavaScript files' array to be included
		 *
		 * @var array
		 */
		public $javascript = array();
		
		/**
		 * Components files' array to be included
		 *
		 * @var array
		 */
		public $components = array();

		/**
		 * The data passed to the View
		 *
		 * @var array
		 */		
		public $data = array();
		
		/**
		 * Default theme
		 *
		 * @var string
		 */
		public $default_theme = 'default';
		
		/**
		 * PHP request variables
		 * @var array
		 */
		public $request;
		
		/**
		 * defines whether the controller is to be cached
		 *
		 * @var boolean
		 */
		public $isCached = true;
		
		/**
		* Loads view by parsed url
		*
		* @param array $parsed_url parsed url
		* @return object $buf contents of loaded view or false
		*/
		function LoadView($parsed_url)
		{
			if ($parsed_url['action'] == null)
			{
				$parsed_url['action'] = 'index';
			}
			//Call action in current controller
			$view = call_user_func_array(array($this, $parsed_url['action']),$parsed_url['parameters']);
			$view = $view?VIEWS.SEPARATOR.$parsed_url['controller'].SEPARATOR.$view.'.php':
						  VIEWS.SEPARATOR.$parsed_url['controller'].SEPARATOR.$parsed_url['action'].'.php';

			if (file_exists($view))
			{
				ob_start();
				Logger::Debug("View ($view) including started");
				include_once($view);
				Logger::Debug("View ($view) including ended");
				$buf = ob_get_contents();
				ob_end_clean();
				if (PARSE_VIEW) 
				{
					$buf = $this->ParseView($buf);
				}
				return $buf;
			}
			else
			{
				return false;
			}
		}
		
		/**
		* Parses view
		*
		* @param object $view view
		* @return mixed view
		*/
		function ParseView($view)
		{
			return $view;
		}
		
		/**
		* Register variable to browser session
		*
		* @param string $var variable name
		* @param string $value value of variable
		*/
		function SessionReg($var, $value)
		{
			$_SESSION[$var] = $value;
		}
		
		/**
		* Includes theme element
		*
		* @param string $element name of theme element
		*/
		function IncludeThemeElement($element)
		{
			include_once(THEMES.SEPARATOR.$this->theme.SEPARATOR.'elements'.SEPARATOR.$element.'.php');
		}	
		
		/**
		* Includes an element
		*
		* @param string $element: element name
		*/
		function IncludeElement($element)
		{
			include_once(ELEMENTS.SEPARATOR.$element.'.php');
		}	

		/**
		* Includes a class
		*
		* @param string $class_name class name
		*/
		function IncludeClass($class_name)
		{
			include_once(CLASSES.SEPARATOR.$class_name.'.php');
		}

		/**
		* Includes a class file
		*
		* @param string $class_name class name
		*/
		function IncludeClassFile($filename)
		{
			include_once(CLASSES.SEPARATOR.$filename.'.php');
		}
		
		/**
		* Includes a class file
		*
		* @param string $class_name class name
		*/
		function IncludeComponentFile($filename)
		{
			require_once(COMPONENTS.SEPARATOR.$filename.'.php');
		}		
		
		/**
		* Is loaded before any controller and performs common for all controllers actions, 
		* can be replaced in application controller or definite controller
		*
		* @param array $parsed_url: array list of parsed url
		*/
		function BeforeLoad($parsed_url)
		{
		}

		/**
 		* Includes specified model file and creates model object
 		* 
 		* @param string $model_name name of model - should coincide with filename (without .php)
 		* @return Model $model_object model object
 		*/
		function GetModelObject($model_name)
		{
			global $object;
			
			$file_name = strtolower($model_name);
			include_once(MODELS.SEPARATOR.$file_name.'.php');
			$model_object = new $model_name($model_name);
			$model_object->config = $this->controller_model->config;
			$data_config = $model_object->database_config;
			$data_config_array = $model_object->config->$data_config;
			
			//puts dataengine into session if is exists and only one database is used
			if (($_SESSION[framework][dataengine])&&(!$object->config->use_several_databases)) 
			{
				unset($_SESSION[framework][dataengine]->fieldName);
				$model_object->data_engine = $_SESSION[framework][dataengine];
			}
			else
			{
				$model_object->data_engine = new DataEngine($data_config_array);
			}
			
			return $model_object;
		}
		
		/**
		* Is loaded after any controller and performs common for all controllers actions, 
		* can be replaced in global application controller or specified controller
		*
		* @param array $parsed_url parsed url
		*/
		function AfterLoad($parsed_url)
		{
		}
		
		/**
 		* Redirects browser to the specified location
 		* 
 		* @param string $url url to redirect to
 		*/		
		function Redirect($url)
		{
			header("Location: /".$url);
			exit();
		}
		
		/**
		 * Gets a value indicating whether the page is being loaded in response to a client postback, or it was loaded for the first time.
		 */
		function IsPostBack()
		{
			return !empty($this->request->post);
		}
		
		/**
		 * Returns filename to include or false if it should not be included in this page
		 *
		 * @param string $file_string filename with optional modifiers
		 * @return string|boolean
		 */
		function GetIncludeFilename($file_string)
		{
			global $current_controller, $object;
			
			//divides array element into js file name and files where to use it
			$exploded_array = explode('|', $file_string);
			$filename = $exploded_array[0];
			
			//if the file is exluded for the specified action
			if (substr($exploded_array[1], 0, 1) == '^')
			{
				if (substr($exploded_array[1], 1) != $object->parsed_url[action]) return $filename;
				else return false;
			}
			
			//if the file should be included here
			if ((in_array($object->parsed_url[action], $exploded_array))||(empty($exploded_array[1])))
				return $filename;
			else
				return false;
		}
		
		/**
 		* Includes Helper files from current controller's $this->helpers
 		*/		
		function IncludeHelpersFiles()
		{
			global $current_controller, $object;
			
			$controller_helpers = $current_controller->helpers;
			unset($current_controller->helpers);

			foreach ($controller_helpers as $value)
			{
				$filename = $this->GetIncludeFilename($value);
				
				if ($filename)
				{
					//Predefined helpers path
					if (file_exists(HELPERS.SEPARATOR.$filename.'.php'))
					{
						include_once(HELPERS.SEPARATOR.$filename.'.php');
						$helper_file = $filename;
						$helper_name = $filename.'Helper';
						$current_controller->helpers->$helper_file = new $helper_name;
					}
					//User helpers path
					elseif (file_exists(INCLUDES.SEPARATOR.'helpers'.SEPARATOR.$filename.'.php'))
					{
						include_once(INCLUDES.SEPARATOR.'helpers'.SEPARATOR.$filename.'.php');
						$helper_file = $filename;
						$helper_name = $filename.'Helper';
						$current_controller->helpers->$helper_file = new $helper_name;
					}
					Logger::Debug("$filename helper included");
				}
			}			
		}
		
		/**
 		* Includes JavaScript files from current controller's $this->javascript
 		*/		
		function IncludeJavascriptFiles()
		{
			global $object;
			
			//Load controller javascript
			$controller_javascript = $this->javascript;
			foreach ($controller_javascript as $value)
			{
				$filename = $this->GetIncludeFilename($value);
				
				if ($filename)
				{
					//User javascript path
					if (file_exists(JAVASCRIPTPATH.SEPARATOR.$filename.'.js'))
					{
						$path = JAVASCRIPTSRC.SEPARATOR.$filename;
						XHTML::JSBuffer($path);
						
						//DEPRICATED
//                      $this->helpers->html->js($filename);
					}
					Logger::Debug("$filename.js included");
				}
			}
		}
		
		/**
 		* Includes Component files from current controller's $this->components
 		*/	
		function IncludeComponentsFiles()
		{
			//Load controller components
			$controller_components = $this->components;
			foreach ($controller_components as $value)
			{
				$filename = $this->GetIncludeFilename($value);
				
				if ($filename)
				{
					if (file_exists(COMPONENTS.SEPARATOR.$filename.'.php'))
					{
	                    include_once (COMPONENTS.SEPARATOR.$filename.'.php');
					}
					Logger::Debug("$value component included");
				}
			}
		}
		
		/**
		 * List all THEMES' subdirectories
		 * @return array $themes list of themes
		 */
		function ListThemes()
		{
			$dir = THEMES;
			$weeds = array('.', '..', 'CVS');
			$dir = array_diff(scandir($dir), $weeds);
			$i =0;
			foreach ($dir as $key=>$value)
			{
				$themes[$i][id] = $value;
				$themes[$i][name] = $value;
				$i++;
			}
			return $themes;
		}
		
		/**
		 * It is for mysql parameters to functions (such as sort_by or page_offset)
		 */
		function DefaultParameters()
		{
			if (empty($this->request->get[sort_by])) $this->request->get[sort_by] = '';
			if (empty($this->request->get[sort_method])) $this->request->get[sort_method] = '';
			if (empty($this->request->get[page_number])) $this->request->get[page_number] = '';
			if (empty($this->request->get[page_offset])) $this->request->get[page_offset] = '';
		}
	}
?>